<html>
<head><meta charset="utf-8"><title>What are the alignment requirements of transmute_copy? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html">What are the alignment requirements of transmute_copy?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="207353212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/207353212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#207353212">(Aug 18 2020 at 23:07)</a>:</h4>
<p>The docs for <a href="https://doc.rust-lang.org/core/mem/fn.transmute_copy.html">transmute_copy</a> say</p>
<blockquote>
<p>Interprets src as having type &amp;U, and then reads src without moving the contained value.</p>
</blockquote>
<p>However, if <code>&amp;T</code> isn't sufficiently aligned to be a <code>&amp;U</code> reference, does anything bad happen? or is the wording just a little imprecise here and it's actually not a problem?</p>



<a name="207354133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/207354133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#207354133">(Aug 18 2020 at 23:18)</a>:</h4>
<p>The source code uses <code>read_unaligned</code> if <code>U</code> is more aligned than <code>T</code>: <a href="https://doc.rust-lang.org/src/core/mem/mod.rs.html#918-925">https://doc.rust-lang.org/src/core/mem/mod.rs.html#918-925</a></p>



<a name="207357307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/207357307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#207357307">(Aug 19 2020 at 00:07)</a>:</h4>
<p>oh heck i should have checked the source. I figured it was intrinsic and wouldn't have source XD</p>



<a name="207358311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/207358311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#207358311">(Aug 19 2020 at 00:23)</a>:</h4>
<p>Surprisingly it looks like it manages to generate correctly-aligned LLVM load instructions despite using <code>read_unaligned</code>, so I guess it didn't need an intrinsic.</p>



<a name="207360134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/207360134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#207360134">(Aug 19 2020 at 00:54)</a>:</h4>
<p>well in the general case that probably depends on the generic types involved, right?</p>



<a name="207374281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/207374281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#207374281">(Aug 19 2020 at 06:28)</a>:</h4>
<p>Looking at the source is not a great idea though if you wonder what the guarantees are for how this function will behave in the future</p>



<a name="207374297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/207374297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#207374297">(Aug 19 2020 at 06:29)</a>:</h4>
<p>the safety condition re: alignment is that things must be aligned for <code>T</code>, and that's it. I guess that's not explicitly stated because all <code>&amp;T</code> are aligned for <code>T</code>.</p>



<a name="207411138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/207411138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#207411138">(Aug 19 2020 at 13:58)</a>:</h4>
<p>Wouldn't be the worst to document that.</p>



<a name="208189161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/208189161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Konrad Borowski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#208189161">(Aug 27 2020 at 08:17)</a>:</h4>
<p>The requirement of <code>&amp;T</code> being correctly aligned for <code>T</code> is rather obvious (it's a reference, not a pointer), but not having the requirement of <code>&amp;T</code> being aligned for <code>U</code> isn't, and I think it would be a good idea to document that there is no such requirement.</p>



<a name="208189345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/208189345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#208189345">(Aug 27 2020 at 08:19)</a>:</h4>
<p>fair. <span class="user-mention" data-user-id="198039">@Konrad Borowski</span> do you want to make a PR?</p>



<a name="208189459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/208189459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Konrad Borowski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#208189459">(Aug 27 2020 at 08:20)</a>:</h4>
<p>in fact, i'm looking at the documentation, and it's kinda misleading here: "transmuting <code>&amp;T</code> to <code>&amp;U</code> and then reading the <code>&amp;U</code>"</p>



<a name="208189468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/208189468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Konrad Borowski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#208189468">(Aug 27 2020 at 08:20)</a>:</h4>
<p>transmuting to <code>&amp;U</code> implies alignment requirements that aren't here</p>



<a name="208190019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/208190019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#208190019">(Aug 27 2020 at 08:27)</a>:</h4>
<p>good point</p>



<a name="208190240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/208190240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Konrad Borowski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#208190240">(Aug 27 2020 at 08:30)</a>:</h4>
<p>as for writing a PR, probably not, I don't know how I would document this function</p>



<a name="208193746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20are%20the%20alignment%20requirements%20of%20transmute_copy%3F/near/208193746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20are.20the.20alignment.20requirements.20of.20transmute_copy.3F.html#208193746">(Aug 27 2020 at 09:15)</a>:</h4>
<p>I mean you just did ;) but I can also put it on my list. It's just a long list so might take a bit until I get around to it.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>